import { queryPathList,  actionPathAdd, actionPathEdit, actionPathExport,actionPathDel,
  actionPathCuringAdd, queryPathnInfo, queryPDevForPath,
} from '../services/path';
import {queryCuringList} from "../services/equipment";

// function unique(arr) {
//   const res = new Map();
//   return arr.filter((a) => !res.has(a) && res.set(a, 1))
// }

export default {
  namespace: 'path',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    pathcuring:[],
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
      curing_submit:false,
      dev_curing:false,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_path_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryPathList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 批量删除操作
    *action_path_del({ payload }, { call, put }) {
      const response = yield call(actionPathDel, payload);
    },
    // 文件导出
    *action_path_export({ payload }, { call, put }) {
      const response = yield call(actionPathExport, payload);
    },

    // 路线养护点保存
    *action_path_curing({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { curing_submit :true },
      });
      const response = yield call(actionPathCuringAdd, payload);
      yield put({
        type: 'changeLoading',
        payload: { curing_submit :false },
      });
    },



    // 新增路线信息
    *add_path({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionPathAdd,payload);
      if(payload.devIds && response.data && response.data.lineId){
        // 计划页面 新增完后，保存节点
        yield put({
          type: 'action_path_curing',
          payload: {
            lineId : response.data.lineId,
            devIds : payload.devIds
          },
        });
      }
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 修改路线信息
    *edit_path({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionPathEdit,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 获取单个路线详情
    *fetch_path_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryPathnInfo, payload);
      response && (
        yield put({
          type: 'appendDataDetail',
          payload:response.data ,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },


    // 获取路线关联设备
    *fetch_path_equipment({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { dev_curing: true },
      });
      const response = yield call(queryPDevForPath, payload);
      response && (
        yield put({
          type: 'appendPathDev',
          payload: response.data.list,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { dev_curing: false },
      });
    },

    // 计划及人文获取路线关联设备并合并
    *fetch_path_equipment_merge({ payload }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: { dev_curing: true },
      });
      const response = yield call(queryPDevForPath, payload);
      const old_pathcuring=yield select(state => state.path.pathcuring);
      if(response && response.data.list && response.data.list.length>0){
        const new_add = response.data.list.reduce((arr, current) => {
          if (old_pathcuring.some(a =>a.devId==current.devId)) {
            return arr.concat([]);
          } else {
            return arr.concat(current);
          }
        }, []);
        // ab差集,并集
        const unionSet = Array.from(new Set([...old_pathcuring,...new_add]));
        yield put({
          type: 'appendPathDev',
          payload: unionSet,
        });
      }
      yield put({
        type: 'changeLoading',
        payload: { dev_curing: false },
      });
    },



    // 获取列表信息
    *fetch_curing_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryCuringList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 右侧列表 添加到 左侧列表 更新
    *fetch_curingupdate_add({ payload }, { call, put, select }) {
      const old_pathcuring=yield select(state => state.path.pathcuring);
      const new_pathcuring = Array.from([...new Set([...old_pathcuring,...payload])]);
      yield put({
        type: 'appendPathDev',
        payload: new_pathcuring,
      });
    },

    // 左侧列表 删除到 恢复左侧列表
    *fetch_curingupdate_del({ payload }, { call, put,select }) {
      const old_pathcuring = yield select(state => state.path.pathcuring);
      const a = new Set(old_pathcuring);
      const b = new Set(payload);
      const new_pathcuring = Array.from(new Set([...a].filter(x => !b.has(x))));

      yield put({
        type: 'appendPathDev',
        payload: new_pathcuring,
      });
    },

    *fetch_data_clear({ payload }, { call, put }) {
      yield put({type: 'appendDataClear'});
    },

    *fetch_curing_clear({ payload }, { call, put }) {
      yield put({type: 'appendPathDev',payload:[]});
    },

  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },

    appendDataClear(state, action) {
      return {
        ...state,
        resdata:{},
      };
    },


    appendPathDev(state, action) {
      return {
        ...state,
        pathcuring: action.payload,
      };
    },

    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        pathcuring:[],
        loading:{}
      };
    },

  },
};
